package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.dto.PhoneCodeDto;
import com.itheima.dto.PhoneDto;
import com.itheima.service.UserService;
import com.itheima.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author YangJiaHao
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/sendMsg")
    public CommonResult sendMsg(@RequestBody PhoneDto phoneDto){
        userService.sendMsg(phoneDto.getPhone());
        return CommonResult.success("发送验证码成功");
    }

    @PostMapping("/login")
    public CommonResult login(@RequestBody PhoneCodeDto phoneCodeDto){
        LoginVo loginVo = userService.login(phoneCodeDto);
        return CommonResult.success(loginVo);
    }

    @PostMapping("/loginout")
    public CommonResult loginout(HttpServletRequest request){
        String token = request.getHeader("AuthorizationUser");
        userService.clearToken(token);
        return CommonResult.success("退出登录成功");
    }


}
